<?php
	include('include/configs.inc.php');

	if(!empty($_POST['token'])) {
		$pseudo = $_POST['pseudo'];
		$password = $_POST['mdp'];
		$error = '';
		
		if(empty($pseudo) || empty($password)) {
			$error .= "<br/>Tout les champs sont obligatoires, merci de tous les remplir !";
		}
		
		if($error == '') {
			$sql = "SELECT id, nom, password FROM user WHERE nom='".$pseudo."' AND password=SHA1('".$password."')";
			$result = $connect->query($sql);
			$user = $result->fetch_object();
			
			if($result->num_rows < 1) {
				$error = "Couple login/password introuvable !";
			} else {
				$_SESSION['UID'] = $user->id;
				$_SESSION['nom'] = $user->nom;
				
				header('Location: '.$_POST['referrer']);
			}
		}
	}
	
	include('template/connexion.html');
	
	$connect->close();
?>